<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="收款登记">

    <!--suppress ALL -->
    <form id="searchForm" class="form-panel" action="${basepath}/rest/manage/contract/selectContractPage" method="get">
        <ul class="panel-content">
            <li>
                <div class="form-actions">
                    <div class="control-group controls span5">
                        <input type="text" class="input-normal  input-pad" name="name" placeholder="请输入关键字搜索">
                    </div>
                    <div class="form-actions  controls">
                        <button type="submit" class="button  button-primary">
                            查询
                        </button>
                    </div>

                </div>
            </li>
        </ul>
    </form>
    <div id="grid"></div>
<#----------------------------------------------操作start--------------------------------------------------------->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal"
          action="${basepath}/rest/manage/finance/insertFinance" method="post">
        <input type="hidden" class="input-normal control-text" name="contractID" id="contractID"/>
        <input type="hidden" class="input-normal control-text" name="proxyID" id="proxyID"/>
        <div class="row">
            <div class="control-group" id="selectType-group">
                <label class="control-label" style="width: 130px;">
                    选择收款类型：
                </label>
                <div class="controls bui-form-group-select">
                    <select class="span7" name="type" id="selectType">
                        <option value="">选择收款类型</option>
                        <option value="proxy">委托书收款</option>
                        <option value="contract">合同收款</option>
                    </select>
                </div>
            </div>
            <div class="control-group span16" id="proxy-sno">
                <label class="control-label" style="width: 120px;">
                    选择委托书编号：
                </label>
                <div class="controls bui-form-group-select">

                        <input type="text" id="sno"  class="span7">
                        <div class="autoName" id="proScreening">

                    </div>
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label name-label" style="width: 120px;">委托书名称：</label>

                <div class="controls span7">
                    <input type="text" class="span7 control-text" id="name" disabled>
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label money-label" style="width: 120px;">委托书金额：</label>

                <div class="controls span7">
                    <input type="text" class="span7 control-text" id="money" disabled>
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label getMoney-label" style="width: 120px;">已收款金额：</label>

                <div class="controls span7">
                    <input type="text" class="span7 control-text" id="getMoney" disabled>
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label" style="width: 120px;">此次收款金额：</label>

                <div class="controls">
                    <input type="text" class="control-text span7" name="income" data-rules="{required : true}"
                           placeholder="请填写此次收款金额">
                </div>
            </div>
        </div>
        <div class="control-group span16">
            <label class="control-label" style="width: 120px;">
                备注：
            </label>
            <div class="controls  control-row-auto " style="margin-left: 0px;">
                <textarea name="remark" placeholder="请输入内容" class="span7"></textarea>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------操作 end---------------------------------------------------------->
<script>
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            Select = BUI.Select,
            columns = [
                {
                    title: '序号', elCls: 'center',
                    dataIndex: 'id',
                    width: '10%',
                    renderer: function (value, obj, index) {
                        return index + 1;
                    }
                },
                {title: '客户名称', dataIndex: 'customerName', elCls: 'center', width: '12%'},
                {title: '合同名称', dataIndex: 'name', elCls: 'center', width: '12%'},
                {title: '合同编号', dataIndex: 'sno', elCls: 'center', width: '10%'},
                {title: '合同金额', dataIndex: 'contractMoney', elCls: 'center', width: '10%'},
                {title: '收款金额', dataIndex: 'contractGetMoney', elCls: 'center', width: '10%'},
                {
                    title: '委托书信息', dataIndex: 'id', elCls: 'center', width: '12%', renderer: function (value) {
                        return '<a href="${basepath}/rest/manage/finance/toProxyList?contractID=' + value + '">查看委托书信息</a>';
                    }
                },
                {
                    title: '收款记录', dataIndex: 'id', elCls: 'center', width: '10%', renderer: function (value) {
                        return '<a href="${basepath}/rest/manage/finance/toRecordList?contractID=' + value + '">查看收款记录</a>';
                    }
                },
                {
                    title: '操作',
                    dataIndex: 'id',
                    elCls: 'center',
                    width: '14%',
                    renderer: function (value, obj, index) {
                        return '<a href="javascript:edit(' + value + ')">登记收款</a>';
                    }
                }
            ];

    var store = new Store({
                url: '/rest/manage/contract/selectContractPage',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'list',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });

    grid.render();

    /*****************************操作*************************************************/
    var contractID="";
    function edit(id) {
        editForm.clearFields();
        editDialog.show();
        $("#contractID").val(id);
        contractID=id;
    }

    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    }).render();

    var editDialog = new BUI.Overlay.Dialog({
        title: '操作',
        width: 560,
        height: 450,
        contentId: 'editContent',
        success: function () {
            if (editForm.isValid()) {
                var last=$("#money").val()-$("#getMoney").val();
                if($("input[name='income']").val()>last){
                    BUI.Message.Alert('出款金额已超出实际金额', function () {
                    }, 'info');
                    return false;
                }
                editForm.submit();
            } else {
                editForm.valid();
            }
        }
    });

    /**********************选择类别,如果是委托书收款的，出现委托书的信息，如果是合同信息是合同的信息**************************/

    $("#selectType").change(function () {
        if ($(this).val() == 'proxy') {
            $("#proxy-sno").remove();
            var html = '<div class="control-group span16" id="proxy-sno"> <label class="control-label" style="width: 120px;">选择委托书编号： </label> <div class="controls bui-form-group-select">  <input type="text" id="sno"  class="span7">\n' +
                    '<div class="autoName" id="proScreening"></div> </div>';
            $("#selectType-group").after(html);
            $('#sno').focus(function () {
                $('#proScreening').show();
                querySelectSno();
            });

        } else {
            $("#proxy-sno").remove();
            queryContract();
        }
    })


    /*********************************多选框查询委托书编号以及相对应的编号的委托书其他信息***********************************/
    function querySelectSno() {
        $.ajax({
            type: 'GET',
            url: '${basepath}/rest/manage/proxy/selectAllList',
            dataType: 'json',
            data:{
                contractID:contractID
            },
            success: function (data) {
                var list = data.data, html = '';
                $.each(list, function (i, e) {
                    html += '<div class="auto-proxy auto-screening" data-id="' + e.id + '">' + e.sno + '</div>'
                });
                $("#proScreening").html(html);
                //点击模糊查询的名称
                $(".auto-screening").click(function () {
                    var name = $(this).text(),
                            id = $(this).attr('data-id');
                    $("#sno").val(name);
                    $("#proxyID").val(id);
                    queryProxyDetail(id);
                    $("#proScreening").hide();
                })
            }
        });
    }


    function queryProxyDetail(id) {
        $.ajax({
            type: 'GET',
            url: '/rest/manage/proxy/selectDetail',
            dataType: 'json',
            data: {
                id: id
            },
            success: function (data) {
                $("#name").val(data.data.name)
                $("#money").val(data.data.proxyMoney)
                $("#getMoney").val(data.data.proxyGetMoney)
                $("#name-label").text('委托书名称')
                $("#money-label").text('委托书金额');
            }
        });
    }
    //查询单个合同信息
    function queryContract() {
        $.ajax({
            type: 'GET',
            url: '/rest/manage/contract/selectDetail',
            dataType: 'json',
            data: {
                id: contractID
            },
            success: function (data) {
                $("#name").val(data.data.name)
                $("#money").val(data.data.contractMoney)
                $("#getMoney").val(data.data.contractGetMoney)
                $(".name-label").html('合同名称')
                $(".money-label").html('合同金额');
            }
        });
    }
    /**********************模糊搜索*************************/
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
</script>

</@page.pageBase>
